package com.easy.base.api.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.easy.base.api.model.dto.ResultBody;
import com.easy.base.api.model.enums.ShowType;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Api(tags = { "error" })
@Controller
public class CustomErrorController implements ErrorController {

    @ApiOperation(value = "全局错误", nickname = "error")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "", response = ResultBody.class)
    })
    @RequestMapping(value = "/error", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<ResultBody<Object>> error(HttpServletRequest request, HttpServletResponse response) {
        int code = response.getStatus();
        HttpStatus status = HttpStatus.valueOf(code);
        return ResponseEntity.internalServerError()
                .body(ResultBody.Error(false, String.valueOf(code), status.getReasonPhrase(), ShowType.MessageError));
    }
}
